package com.shenmazong.demodelayqueuetask.service;

import com.shenmazong.demodelayqueuetask.utils.MqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: server-java-demo
 * @description: Producr
 * @author: 亮子说编程
 * @create: 2020-11-02 16:01
 **/

@Slf4j
@Service
public class ProductService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(String msg, long time, String delayQueueName) {
        //rabbit默认为毫秒级
        long times = time * 1000;
        MessagePostProcessor processor = new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(String.valueOf(times));
                return message;
            }
        };

        // 拼装msg
        msg = msg + ":" + delayQueueName;
        amqpTemplate.convertAndSend(MqConstant.MY_EXCHANGE, MqConstant.DEAD_LETTER_QUEUE, msg, processor);
    }
}
